﻿using apiframworkTest.Common;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Http.Cors;

namespace apiframworkTest
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {

            //设置跨域
            config.EnableCors();//全部不限制 new EnableCorsAttribute("*", "*", "*")

            //EnableCorsAttribute attribute = new EnableCorsAttribute("http://localhost:9008/", "*", "*");
            //attribute.Origins.Add("http://localhost:9009/");
            //config.EnableCors(attribute);

            //返回数据改成Json
            GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();

            //HttpContext.Current.Response.Headers.Add("Access-Control-Allow-Credentials", "true");

            // Web API 配置和服务

            // Web API 路由
            config.MapHttpAttributeRoutes();
            config.Routes.Add("test", new MyRout());
            
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
    }
}
